<?php

class Zax_App_Default_Dir
{
	protected $_dirname;
	protected $_path;
	protected $_actions = array();
	
	
	final function Dir_Load($path)
	{
		global $Zax;
		if (substr_count($path,'\\') !== 0) die('!\\');
		
		$path = explode('/',$path);
		$filename = array_pop($path);
		$dir = implode('/',$path);
		$dir = strtr($dir,'.','_');
		
		$this->_path = $dir.'/'.$filename;
		$this->_dirname = ZAX_DATA_PATH.'/'.$dir.'/'.$filename; 
		//$this->_writing_allowed = $Zax->acl->get($path,'w');
		return true;
	}
	
	final function Dir_getSubdirs($sorting = null)
	{	//TODO: Code crade à dégager lorsque l'indexation sera prête
		require_once 'ninja.php';
		return dir_list($this->_dirname);
	}
	
	final function Dir_getFiles($sorting = null)
	{	//TODO: Code crade à dégager lorsque l'indexation sera prête
		require_once 'ninja.php';
		return file_list($this->_dirname);
	}
	
	/*
	 * Méthodes de configuration de l'Application
	 */
	
	final function App_setAction($id,$name,$writes)
	{
		$action = array();
		$App_name = str_replace("Zax_App_",'',get_class($this));
		$file1 = ZAX_SYSTEM_PATH."/Apps/$App_name/action_$id.icon.png";
		$file2 = ZAX_SYSTEM_PATH."/Apps/Default_Dir/action_$id.icon.png";
		if(file_exists($file1))
			$action['icon'] = "Zax/Apps/$App_name/action_$id.icon.png";
		elseif(file_exists($file2))
			$action['icon'] = "Zax/Apps/Default_Dir/action_$id.icon.png";
		else
			$action['icon'] = null;
			
		$action['name'] = $name;
		$action['id'] = $id;
		$action['writes'] = $writes;
		
		$this->_actions[] = $action;
	}
	
	final function App_getActionsToolbar()
	{

		$toolbar = '<ul class="app_toolbar">';
		foreach($this->_actions as $action)
		{
			//if($Zax->acl->get($this->_path,$action['id']))
			if(1)
			{
				//style="background-image:url('.$action['icon'].')"
				$toolbar.= '<li>';
				$toolbar.= '<a  style="background-image:url('.$action['icon'].');" 
							class="button" href="?'.$this->_path.':'.$action['id'].'">';
				$toolbar.= $action['name'].'</a></li>';
			}
		}
		$toolbar.= '</ul>';
		
		return $toolbar;
	}
	
	/*
	 * Actions de bases (peuvent être surchargées)
	 */
	
	public function Action_explore()
	{
		$subdirs = $this->Dir_getSubdirs();
		$files = $this->Dir_getFiles();

		$output = "<h2>Répertoires</h2><ul>\n";
		foreach($subdirs as $dir)
		{
			$output.= '<li><a href="?'.$this->_path."$dir/:explore\">".$dir.'</a></li>';
		}
		
		$output .= "<h2>Fichiers</h2><ul>\n";
		
		foreach($files as $file)
		{
			$output.= '<li><a href="?'.$this->_path."$file\">".$file.'</a></li>';
		}
		
		return $output;
	}
}